import os
from flask import current_app
from sqlalchemy import desc
from applications.extensions import db
from applications.extensions.init_upload import photos
from applications.models import Photo
from applications.schemas import PhotoOutSchema
from applications.common.curd import model_to_dicts
import uuid
import datetime


def get_photo(page, limit):
    photo = Photo.query.order_by(desc(Photo.create_time)).paginate(page=page, per_page=limit, error_out=False)
    count = Photo.query.count()
    data = model_to_dicts(schema=PhotoOutSchema, data=photo.items)
    return data, count



def upload_one(photo, mime):
    # 获取当前时间的时间戳，用于文件名的一部分（可选）
    timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    # 生成UUID字符串
    unique_id = uuid.uuid4()
    name, exp = photo.filename.rsplit(".", 1)
    # 带有UUID和时间戳的新文件名
    new_filename = f"{name}_{timestamp}_{unique_id}.{exp}"

    # # filename = photos.save(photo)
    filename = photos.save(storage=photo, name=new_filename)
    file_url = '/_uploads/photos/' + filename
    # file_url = photos.url(filename)
    upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
    size = os.path.getsize(upload_url + '/' + filename)
    photo = Photo(name=filename, href=file_url, mime=mime, size=size)
    db.session.add(photo)
    db.session.commit()
    return file_url


def delete_photo_by_id(_id):
    photo_name = Photo.query.filter_by(id=_id).first().name
    photo = Photo.query.filter_by(id=_id).delete()
    db.session.commit()
    upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
    os.remove(upload_url + '/' + photo_name)
    return photo
